const app = getApp();
const request = require('../../../utils/api.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    notPic: true, //是否有选择文件
    havePic: false,
    qiniutoken: "",
    pictureNum: 0, //选择照片的数量
    pictures: [], //储存图片地址
    pictureFormEdit:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getQiniuToken();
    let that=this;
    
    console.log(this.data.qiniutoken + "虎猫鼠")
  },

  // 获取七牛token
  getQiniuToken() {
    const data = {
      url: '/v3/qiniu/token',
      method: "POST",
      param: {
        "fileType": 2
      }
    }
    request(data).then(success => { // 获取qiniu上传文件token
      if (success.data.code === 200) {
        this.setData({
          qiniutoken: success.data.data.token
        })
        console.log(success.data.data.token)
      }
    }).catch(error => {
      console.log(error)
    })
  },

  //选择照片，并跳转至编辑页面
  choiceType(e) {
    var that = this
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        // that.previewPic(tempFilePaths)
        wx.navigateTo({
          url: '../' + e.currentTarget.dataset.page + '/' + e.currentTarget.dataset.page + '?path=' + tempFilePaths
        })
      }
    })
  },



  deletePicture: function(e) {
    const that = this;
    console.log(e.currentTarget.dataset.page)
    this.data.pictures.forEach(arr => {

      if (arr == e.currentTarget.dataset.page) {

        //获取元素下标，并根据下标删除该元素
        let position = that.data.pictures.indexOf(arr);
        that.data.pictures.splice(position, 1);
        that.setData({
          'pictures': that.data.pictures,
          'pictureNum': that.data.pictures.length
        })
      }
    })

  },

  previewImage: function(e) {
    var current = [e.currentTarget.dataset.src]
    wx.previewImage({
      current: current,
      urls: current
    })
  },



  picSet: function(e) {
    wx.navigateTo({
      url: '../' + e.currentTarget.dataset.page + '/' + e.currentTarget.dataset.page +'?pictureNum=' + this.data.pictureNum+'&pictures='+this.data.pictures
    })
  },

  //按钮无响应提示框
  btnTap: function() {
    wx.showToast({
      title: "请添加要打印的图片",
      icon: "none",
      duration: 1500
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that=this;
    if (that.data.pictureFormEdit!=''){//获取来自子界面的图片地址，并删除脏数据
      that.data.pictures.push(that.data.pictureFormEdit);
      that.data.pictures.forEach(arr => {
        if (arr == '' || arr == null) {
          let position = that.data.pictures.indexOf(arr);
          that.data.pictures.splice(position, 1);
        }
      })
      that.setData({
        pictures: that.data.pictures,
        pictureFormEdit: '',
        pictureNum: that.data.pictures.length
      })
    }
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})